package net.wang.cloud.ticket.repo;


import net.wang.cloud.ticket.domain.Ticket;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TicketRepo extends JpaRepository<Ticket, Long> {
    List<Ticket> findByOwner(long owner);

    Ticket findByTicketNumber(long ticketNumber);

    @Modifying
    @Query(value = "UPDATE Ticket SET lockUser = ?1 WHERE lockUser < 1 AND ticketNumber = ?2")
    int lockTicket(long customerId, long ticketNumber);

    @Modifying
    @Query(value = "UPDATE Ticket SET owner = ?1 WHERE lockUser < 1 AND ticketNumber = ?2")
    int moveTicket(long customerId, long ticketNumber);

    @Modifying
    @Query(value = "UPDATE Ticket SET lockUser = 0 WHERE owner = ?1 AND ticketNumber = ?2")
    int unMoveTicket(long customerId, long ticketNumber);

    @Modifying
    @Query(value = "UPDATE Ticket SET lockUser = 0 WHERE lockUser =?1 AND ticketNumber = ?2")
    int unLockTicket(long customerId, long ticketNumber);

    Ticket findByTicketNumberAndLockUser(long ticketNumber, long customerId);
}
